package com.example.springbootelasticsearch;

import lombok.extern.log4j.Log4j2;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;

@SpringBootApplication
@RestController
@Log4j2
public class SpringbootElasticsearchApplication {

	@Autowired
	private TransportClient client;

	@GetMapping("/")
	public String index(){
		return "index";
	}

	/**
	 * 根据ID查询对应的图书
	 * 如GET请求 http://localhost:8080/get/culture/book?id=5
	 * @param id
	 * @return
	 */
	@GetMapping("/get/culture/book")
	public ResponseEntity get(@RequestParam(name = "id",defaultValue = "") String id){
		if(id.isEmpty()){
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}
		GetResponse result = this.client.prepareGet("culture","book",id).get();
		if(!result.isExists()){
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}

		return new ResponseEntity(result.getSource(), HttpStatus.OK);
	}

	/**
	 * 增加图书的方法
	 * POST请求 http://localhost:8080/add/culture/book
	 * Postman选中BODY->form-data，然后添加表单数据。（这应该也适用模拟表单提交）
	 * 可以多次提交添加同一条数据。
	 * 返回新增数据的ID
	 * @param
	 */
	@PostMapping("/add/culture/book")
	public ResponseEntity add(
			@RequestParam(name = "book_name") String book_name,
			@RequestParam(name = "author") String author,
			@RequestParam(name = "classify") String classify,
			@RequestParam(name = "book_words") int book_words,
			@RequestParam(name = "publish_house") String publish_house,
			@RequestParam(name = "public_time")
			@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
					Date public_time
	){
		try {
			XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
					.startObject()
					.field("book_name",book_name)
					.field("author",author)
					.field("classify",classify)
					.field("book_words",book_words)
					.field("publish_house",publish_house)
					.field("public_time",public_time.getTime())
					.endObject();

			IndexResponse indexResponse = this.client.prepareIndex("culture","book")
					.setSource(xContentBuilder)
					.get();
			return new ResponseEntity(indexResponse.getId(),HttpStatus.OK);
		}catch (IOException e){
			e.printStackTrace();
			return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
		}

	}

	/**
	 * 删除图书的方法
	 * DELETE请求 http://localhost:8080/delete/culture/book?id=vt5ZEWABEfBDa4D83bhT
	 * 删除成功返回DELETE,删除失败返回NOT_FOUND
	 * @param
	 */
	@DeleteMapping("delete/culture/book")
	public ResponseEntity delete(@RequestParam(name = "id") String id){
		if (StringUtils.isEmpty(id)) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}
		DeleteResponse result = this.client.prepareDelete("culture","book",id)
				.get();
		return new ResponseEntity(result.getResult().toString(), HttpStatus.OK);
	}

	public static void main(String[] args) {
		SpringApplication.run(SpringbootElasticsearchApplication.class, args);
	}
}
